package geguol.cc.widgetapplication.feature;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import geguol.cc.widgetapplication.R;

public class TaskControllerActivity extends AppCompatActivity {
	@BindView(R.id.button5)
	Button pause;//暂停
	@BindView(R.id.button6)
	Button resume;//重启
	@BindView(R.id.start)
	Button start;
//===============================//
	@BindView(R.id.start1)
	Button start1;//启动任务
	@BindView(R.id.cancle)
	Button cancle;//取消任务
	private FutureTask<Number> futureTask;
	private ExecutorService es;
	private PasusableThreadPoolExecutor pausableThreadPoolExecutor;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task_controller);
		ButterKnife.bind(this);
		initFetureTask();
		execPauseThread();
	}

	private void initFetureTask() {
		es = Executors.newSingleThreadExecutor();
		CountCallable countCallable = new CountCallable();
		futureTask = new FutureTask<Number>(countCallable){
			@Override
			protected void done() {
				try {
					Number number = futureTask.get();
					System.out.println("任务结束了"+number.getNum());
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}catch (CancellationException e){
					System.out.println("任务被取消");
				}

			}
		};

	}

	public void execPauseThread(){
		pausableThreadPoolExecutor = new PasusableThreadPoolExecutor(1, 10, 0L,
				TimeUnit.SECONDS, new PriorityBlockingQueue<>());
		for (int i = 1; i <= 10; i++) {
			final int priority = i;
			pausableThreadPoolExecutor.execute(new PriorityRunnable(priority) {
				@Override
				public void doSth() {
					System.out.println("优先级 "+priority);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}
	}

	@OnClick({R.id.cancle, R.id.button5, R.id.button6, R.id.start, R.id.start1})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.cancle:
				futureTask.cancel(true);
				break;
			case R.id.button5:
				// 暂停还未开始执行的线程
				// 正在执行的线程并不受控制(因为所有逻辑只是在开始之前进行控制的)
				pausableThreadPoolExecutor.pause();
				break;
			case R.id.button6:
				pausableThreadPoolExecutor.resume();
				break;
			case R.id.start:
//				execPauseThread(); 活跃线程只有一个
				System.out.println("pausableThreadPoolExecutor.getActiveCount()"+pausableThreadPoolExecutor.getActiveCount());
				System.out.println("pausableThreadPoolExecutor.getTaskCount()"+pausableThreadPoolExecutor.getTaskCount());
				break;
			case R.id.start1:
				es.execute(futureTask);
				System.out.println("任务启动");
				break;
		}
	}



	public static class CountCallable implements Callable<Number> {

		@Override
		public Number call() throws Exception {
			Number number = new Number();
			System.out.println("运行在"+Thread.currentThread().getName());
			Thread.sleep(5000);
			number.setNum(10);
			return number;
		}

	}

	static class Number {
		private int num;

		private int getNum() {
			return num;
		}

		private void setNum(int num) {
			this.num = num;
		}
	}
}
